home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 43.zip / Sources C- WorkDisk V.adf / dos / mkdirs.c < prev    next >
C/C++ Source or Header  |  1987-02-16  |  3KB  |  122 lines

  1. /*
  2. * =====================================================================
  3. *
  4. *   Program : MKDIRS or MakeDirectories.    Date : July 18th 1987.
  5. *
  6. *   Author : J. Van Houtven.
  7. *
  8. * =====================================================================
  9. */
  10.  
  11.  
  12. #include "libraries/dosextens.h"
  13.  
  14. # define MAX_NR_OF_SUBDIRS  200
  15.  
  16. /* Only UnLock() locks that are obtained with Lock() or DupLock() (?) !!!! */
  17.  
  18. extern struct FileLock *CreateDir(), *CurrentDir();
  19.  
  20. struct FileLock *next_lock[MAX_NR_OF_SUBDIRS+1],
  21.                 *current_lock,
  22.                 *first_lock;
  23.  
  24. int  i, nr_of_subdirs;
  25.  
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.  char  *dirname, *drivename;
  32.  struct FileLock *create_lock;
  33. void die(), CloseALL();
  34.  
  35.  if(argc>1)
  36.   drivename = argv[1];
  37.  else
  38.  {
  39.   puts("Usage : MKDIRS <drive> <nr_of_subdirs> <name of subdirs>.\n");
  40.   puts("<drive>           : 'df1:', 'df2:' or 'df3:'");
  41.   puts("<nr_of_subdirs>   : INTEGER NUMBER between [1-200], DEFAULT = 50");
  42.   puts("<name of subdirs> : CHARACTER STRING, DEFAULT = \"  * BAMIGA SECTOR ONE *  \"\n");
  43.   exit(0);
  44.  } 
  45.  
  46.  if(argc>2)
  47.   {
  48.    nr_of_subdirs = atoi(argv[2]);
  49.    if((nr_of_subdirs <1) || (nr_of_subdirs>MAX_NR_OF_SUBDIRS))
  50.    {
  51.     puts("<nr_of_subdirs> must be within [1-200] !\n");
  52.     exit(0);
  53.    }
  54.   }
  55.  else
  56.   nr_of_subdirs = 50;
  57.  
  58.  if(argc>3)
  59.   dirname = argv[3];
  60.  else
  61.   dirname = "  * BAMIGA SECTOR ONE *  ";
  62.  
  63.  /* ====== mkdirs routine ======  */
  64.  
  65.  /* init next_lock[] */
  66.  for(i=0;i<=nr_of_subdirs;i++) next_lock[i] = 0;
  67.  
  68.  first_lock = CurrentDir(0);
  69.  CurrentDir(first_lock);
  70.  
  71.  if( (current_lock = Lock(drivename,ACCESS_WRITE) ) == 0)
  72.  {
  73.   die("Couldn't get a lock on specified drive !\n");
  74.  }
  75.  
  76.  next_lock[0] = current_lock;
  77.  
  78.  for(i=0;i<nr_of_subdirs;i++)
  79.  {
  80.   CurrentDir(next_lock[i]);
  81.  
  82.   /* Test if "dirname" already exists ... */
  83.   /* BUG : We test this by trying to obtain a lock on "dirname"
  84.      but "dirname" could still be a normal file ! */
  85.   if((create_lock = Lock(dirname,ACCESS_WRITE)) !=0)
  86.   {
  87.    UnLock(create_lock);
  88.    die("Subdirectory already exists !\n");
  89.   }
  90.  
  91.   if((create_lock = CreateDir(dirname)) == 0)
  92.    die("Couldn't create subdirectory !\n");;
  93.   UnLock(create_lock);
  94.  
  95.   if((next_lock[i+1] = Lock(dirname,ACCESS_WRITE)) == 0)
  96.    die("Couldn't obtain a lock on subdirectory\n");
  97.  
  98.  }
  99.  
  100.  CloseALL();
  101.  
  102. } /* ====== end main ====== */
  103.  
  104. void CloseALL()
  105. {
  106.  
  107.  for(i=nr_of_subdirs;i>=1;i--) if(next_lock[i]) UnLock(next_lock[i]);
  108.  
  109.  CurrentDir(first_lock);
  110.  
  111.  if(current_lock) UnLock(current_lock);
  112.  
  113.  
  114. void die(errormsg)
  115. char *errormsg;
  116. {
  117.  CloseALL();
  118.  puts(errormsg);
  119.  exit(0);
  120. }
  121.